    //
//  ConfigViewController.m
//  AgriMobile
//
//  Created by 春名 啓作 on 11/05/14.
//  Copyright 2011 e-agri. All rights reserved.
//

#import "ConfigViewController.h"
#import "HttpMethod.h"

@implementation ConfigViewController

NSString *authResult = nil;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSLog(@"ConfigView - viewDidLoad");

    [super viewDidLoad];
	
	NSUserDefaults *udef = [NSUserDefaults standardUserDefaults];
	NSLog(@"email : %@", [udef stringForKey:@"email"]);
	if ([udef stringForKey:@"email"] != nil) {
		txtEmail.text = [udef stringForKey:@"email"];
		txtPassword.text = [udef stringForKey:@"password"];
		//tglDevEnv.on = [udef boolForKey:@"devenv"];
	}else {
		NSMutableDictionary *def = [NSMutableDictionary dictionary];
		[def setObject:@"email" forKey:@"email"];
		[def setObject:@"password" forKey:@"password"];
		//[def setObject:TRUE forKey:@"devenv"];
		[udef registerDefaults:def];
		//[def release];
	}
	//[udef release];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (IBAction)btnPressed {

	NSLog(@"ConfigView - btnPressed");

	[txtEmail resignFirstResponder];
	[txtPassword resignFirstResponder];
	[actIndicator startAnimating];

	authResult = nil;
	[NSThread detachNewThreadSelector:@selector(testAuth) toTarget:self withObject:nil];
}


- (void)testAuth {
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	//@synchronized(self){
	
		HttpMethod *http = [HttpMethod alloc];
		NSString *URL_Root = URLRoot;
		NSString *sessId = [http doGet:[[NSString alloc]initWithFormat:
										@"%@/wap?login=%@&pwd=%@", URL_Root, txtEmail.text, txtPassword.text]];
		NSLog(@"sessId = %@", sessId);
		
		if (sessId == nil || [sessId isEqualToString:@"FAIL"]) {
			authResult = @"FAIL";
		}else {
			authResult = @"OK";
			
		  if (tglAutoLogin.on) {
			NSUserDefaults *udef = [NSUserDefaults standardUserDefaults];
			[udef setObject:txtEmail.text forKey:@"email"];
			[udef setObject:txtPassword.text forKey:@"password"];
			//[udef setObject:[tglAutoLogin.on intValue] forKey:@"devenv"];
			[udef synchronize];
			//[udef release];
			//[http doGet:[[NSString alloc] initWithFormat:@"%@/wap?logout=y", URL_Root]];
		  }
		}
		[http release];
	//}

	[self performSelectorOnMainThread:@selector(testAuthDone) withObject:nil waitUntilDone:NO];
	[pool release];
	[NSThread exit];
}

- (void)testAuthDone {
	[actIndicator stopAnimating];

	UIAlertView *alert = [[UIAlertView alloc]
						  initWithTitle:@"認証結果"
						  message:authResult
						  delegate:self
						  cancelButtonTitle:@"OK"
						  otherButtonTitles:nil
						  ];
	[alert show];
	[alert release];
	[authResult release];
}

- (void)didReceiveMemoryWarning {
	NSLog(@"ConfigView - didReceiveMemoryWarning");
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
	NSLog(@"ConfigView - viewDidUnload");
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

}


- (void)dealloc {
	NSLog(@"ConfigView - dealloc");

	[txtEmail release];
	[txtPassword release];
	//[btnTest release];
	[tglAutoLogin release];
	[actIndicator release];

    [super dealloc];
}


@end
